package com.jia;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.jia.activity.*;

/**
 * <></>
 */
public class MyActivity extends Activity {

    private LinearLayout nameContainer;
    private LinearLayout addressContainer;
    private LinearLayout parentContainer;

    /**
     * @param savedInstanceState s
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

//        Resources res = this.getResources();
//        String s1 = res.getQuantityString(R.plurals.egg_in_a_nest_test, 0, 0);
//        String s2 = res.getQuantityString(R.plurals.egg_in_a_nest_test, 1, 1);
//        String s3 = res.getQuantityString(R.plurals.egg_in_a_nest_test, 2, 2);
//        String s4 = res.getQuantityString(R.plurals.egg_in_a_nest_test, 10, 10);
//        System.out.println("s1 = " + s1 + "s2 = " + s2 + "s3 = " + s3 + "s4 = " + s4);


//        TextView textView = (TextView) this.findViewById(R.id.stringText);

        //Read a simple string and set it in a test_xml view
//        String simpleString = this.getString(R.string.simple_string);
//        textView.setText(simpleString);

        //Read a quoted string and set it in a test_xml view
//        String quotedString = this.getString(R.string.quoted_string);
//        textView.setText(quotedString);

        //Read a double quoted string and set it in a test_xml view
//        String doubleQuotedString = this.getString(R.string.double_quoted_string);
//        textView.setText(doubleQuotedString);

//        //Read a Java format string
//        String javaFormatString = this.getString(R.string.java_format_string);
//        //Convert the formatted string by passing in arguments
//        String substitutedString = String.format(javaFormatString, "Hello", "Android");
//        //set the output in a test_xml view
//        textView.setText(substitutedString);

        //Read an html string from the resource and set it in a test_xml view
//        String htmlTaggedString = this.getString(R.string.tagged_string);
        //Convert it to a test_xml span so that it can be set in a test_xml view
        //android.test_xml.Html class allows painting of "html" strings
        //This is strictly an Android class and does not support all html tags
//        Spanned textSpan = android.text.Html.fromHtml(htmlTaggedString);
        //Set it in a test_xml view
//        textView.setText(textSpan);

        // Get a drawable
//        GradientDrawable roundedRectangle = (GradientDrawable) this.getResources().getDrawable(
//                R.drawable.my_rounded_rectangle);
//        //Set it as a background to a test_xml view
//        textView.setBackgroundDrawable(roundedRectangle);


        /**
         * 测试读取XML信息
         */
//        TextView xmlTextView = (TextView) this.findViewById(R.id.xmlText);
//        try {
//            xmlTextView.setText(XmlUtil.getEventsFromAnXMLFile(this));
//        }
//        catch (XmlPullParserException e) {
//            e.printStackTrace();
//        }
//        catch (IOException e) {
//            e.printStackTrace();
//        }


//        TextView textTextView = (TextView) this.findViewById(R.id.xmlText);
//
//        textTextView.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                //To change body of implemented methods use File | Settings | File Templates.
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                //To change body of implemented methods use File | Settings | File Templates.
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                //打开指定的简单的Intent
////                IntentsUtils.invokeWebBrowser(MyActivity.this);
//                //通过Component来启动Intent，这里的Activity需要在Manifest中声明
////                Intent intent = new Intent();
////                intent.setComponent(new ComponentName(
////                        "com.android.contacts"
////                        , "com.android.contacts.DialContactsEntryActivity"));
////                MyActivity.this.startActivity(intent);
//            }
//        });

//        try {
//            textTextView.setText(TextFileUtil.getStringFromAssetFile(this));
//        }
//        catch (IOException e) {
//            e.printStackTrace();
//        }

        //给Intent添加额外的数据
//        Intent intent = new Intent();
//        Bundle bundle = new Bundle();
//        intent.putExtras(bundle);

        //根据Intent类型遍历系统中的所有符合条件Intent，然后启动合适的
//        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
//        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//        PackageManager pm = getPackageManager();
//        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
//        for (ResolveInfo ri : list) {
//            //ri.activityInfo.
//            String packagename = ri.activityInfo.packageName;
//            String classname = ri.activityInfo.name;
//            System.out.println(packagename + "    " + classname);
//            if ("com.jxy.music.MyActivity".equals(classname)) {
//                Intent ni = new Intent();
//                ni.setClassName(packagename, classname);
//                this.startActivity(ni);
//            }
//        }

        //Go to home screen 返回桌面
//        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
//        mainIntent.addCategory(Intent.CATEGORY_HOME);
//        startActivity(mainIntent);

        //调用带返回值的Intent
//        invokePick(this);

        //用代码创建布局实例
//        createNameContainer();
//        createAddressContainer();
//        createParentContainer();
//        setContentView(parentContainer);

        //设置URL，email的自动识别
//        TextView tvUrl = (TextView) this.findViewById(R.id.stringText);
//        tvUrl.setAutoLinkMask(Linkify.ALL);
////        Linkify.addLinks(tvUrl, Linkify.ALL);
//        tvUrl.setText("http://www.baidu.com or jixiuya@163.com");

        //自动提示的Text控件
//        AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) this.findViewById(R.id.xmlText);
//        ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
//                new String[]{"English", "Hebrew", "Hindi", "Spanish", "German", "Greek"});
////        autoCompleteTextView.setAdapter(aa);
//
//        MultiAutoCompleteTextView multiAutoCompleteTextView = (MultiAutoCompleteTextView) this.findViewById(R.id.xmlText);
//        multiAutoCompleteTextView.setAdapter(aa);
//        multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        //线性布局
//        Intent intent = new Intent(this,LinearLayoutTestActivity.class);
//        this.startActivity(intent);

        //checkbox
//        Intent intent = new Intent(this, CheckBoxActivity.class);
//        this.startActivity(intent);

        // RadioGroup
//        Intent intent = new Intent(this, RadioGroupActivity.class);
//        this.startActivity(intent);

        // DateTime
//        Intent intent = new Intent(this, DateTimePickerActivity.class);
//        this.startActivity(intent);

        // Map
//        Intent intent = new Intent(this, MapActivity.class);
//        this.startActivity(intent);

        // ListView
//        Intent intent = new Intent(this, ListViewActivity.class);
//        this.startActivity(intent);

        // GridView
//        Intent intent = new Intent(this,GridViewActivity.class);
//        this.startActivity(intent);

        // Spinner
//        Intent intent = new Intent(this,SpinnerActivity.class);
//        this.startActivity(intent);

        // 自定义Adapter
//        Intent intent = new Intent(this,GridViewCustomAdapterActivity.class);
//        this.startActivity(intent);

        // FrameLayout
//        Intent intent = new Intent(this, FrameLayoutActivity.class);
//        this.startActivity(intent);

        // 添加ContexMenu,长按住控件即可显示
//        TextView textView = (TextView) this.findViewById(R.id.stringText);
//        registerForContextMenu(textView);

        // 更改系统配置
//        Intent intent = new Intent(this, ChangeCfgActivity.class);
//        this.startActivity(intent)

        // 消息传递
//        Intent intent = new Intent(this, HandlerTest.class);
//        this.startActivity(intent);

        // 消息传递
//        Intent intent = new Intent(this, CallPrim.class);
//        this.startActivity(intent);

        // 异步任务
//        Intent intent = new Intent(this, AsyncTaskTest.class);
//        this.startActivity(intent);

        // Activity管理
//        Intent intent = new Intent(this, OtherActivity.class);
//        this.startActivity(intent);

        // 数据传输
//        Intent intent = new Intent(this, BundleTest.class);
//        this.startActivity(intent);

        // Intent传输数据
//        Intent intent = new Intent(this, SysAction.class);
//        startActivity(intent);


        // 补间动画
//        Intent intent = new Intent(this, AnimationDrawable.class);
//        startActivity(intent);

        // 属性动画
//        Intent intent = new Intent(this, AnimatorTest.class);
//        startActivity(intent);

        // 属性资源
//        Intent intent = new Intent(this, AttrResTest.class);
//        startActivity(intent);

        // 原始资源
//        Intent intent = new Intent(this, RawResTest.class);
//        startActivity(intent);

        // 鼠标手势
//        Intent intent = new Intent(this, GestureTest.class);
//        startActivity(intent);


    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Sample Context Menu");
        menu.add(200, 200, 200, "item1");
    }

    /**
     * 可以自动处理Action
     *
     * @param target
     */
    public void myClickHandler(View target) {
        switch (target.getId()) {
//            case R.id.button1:
//                Intent intent = new Intent(this, ListViewActivity.class);
//                startActivity(intent);
//                break;
        }
    }

    /**
     * 用代码创建name控件
     */
    private void createNameContainer() {
        nameContainer = new LinearLayout(this);
        nameContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        nameContainer.setOrientation(LinearLayout.HORIZONTAL);
        TextView nameLbl = new TextView(this);
        nameLbl.setText("Name: ");
        TextView nameValue = new TextView(this);
        nameValue.setText("John Doe");
        nameContainer.addView(nameLbl);
        nameContainer.addView(nameValue);
    }

    private void createAddressContainer() {
        addressContainer = new LinearLayout(this);
        addressContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        addressContainer.setOrientation(LinearLayout.VERTICAL);
        TextView addrLbl = new TextView(this);
        addrLbl.setText("Address:");
        TextView addrValue = new TextView(this);
        addrValue.setText("911 Hollywood Blvd");
        addressContainer.addView(addrLbl);
        addressContainer.addView(addrValue);
    }

    private void createParentContainer() {
        parentContainer = new LinearLayout(this);
        parentContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        parentContainer.setOrientation(LinearLayout.VERTICAL);
        parentContainer.addView(nameContainer);
        parentContainer.addView(addressContainer);
    }


    /**
     * 处理带返回值的Intent
     *
     * @param activity <br>
     */
    public void invokePick(Activity activity) {
        Intent pickIntent = new Intent(Intent.ACTION_PICK);
        int requestCode = 1;
        pickIntent.setData(Uri.parse(
                "content://com.google.provider.NotePad/notes"));
        activity.startActivityForResult(pickIntent, requestCode);
    }

    /**
     * <br>
     *
     * @param requestCode  <br>
     * @param resultCode   <br>
     * @param outputIntent <br>
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent outputIntent) {
        //This is to inform the parent class (Activity)
        //that the called activity has finished and the baseclass
        //can do the necessary clean up
        super.onActivityResult(requestCode, resultCode, outputIntent);
        parseResult(this, requestCode, resultCode, outputIntent);
    }

    /**
     * <br>
     *
     * @param activity     <br>
     * @param requestCode  <br>
     * @param resultCode   <br>
     * @param outputIntent <br>
     */
    public void parseResult(Activity activity, int requestCode, int resultCode, Intent outputIntent) {
        if (requestCode != 1) {
            Log.d("Test", "Some one else called this. not us");
            return;
        }
        if (resultCode != Activity.RESULT_OK) {
            Log.d("Test", "Result code is not ok:" + resultCode);
            return;
        }
        Log.d("Test", "Result code is ok:" + resultCode);
        Uri selectedUri = outputIntent.getData();
        Log.d("Test", "The output uri:" + selectedUri.toString());

        //Proceed to display the note
        outputIntent.setAction(Intent.ACTION_VIEW);
        startActivity(outputIntent);
    }
}
